package com.wdciti.sccp.iot.common.aio;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * @author by early
 */
@Data
public class DeploymentResult {

    private String factoryName;
    private boolean complete;
    //    private boolean success;
    private List<DeploymentId> deploymentIds;

    public DeploymentResult(String factoryPrefix) {
        this.factoryName = factoryPrefix;
        this.deploymentIds = new ArrayList<>();
    }

    public void addExcludeDeploy(String verticleName) {
        DeploymentId id = new DeploymentId(verticleName, status.EXCLUDE);
        this.deploymentIds.add(id);
    }

    public void addSuccessDeploy(String verticleName, String deploymentId) {
        DeploymentId id = new DeploymentId(verticleName, status.SUCCESS);
        id.setDeploymentId(deploymentId);
        this.deploymentIds.add(id);
    }

    public void addFailedDeploy(String verticleName, Throwable exception) {
        DeploymentId id = new DeploymentId(verticleName, status.FAILED);
        id.setException(exception);
        this.deploymentIds.add(id);
    }

    public void addDeployment(DeploymentId deploymentId) {
        this.deploymentIds.add(deploymentId);
    }

    public enum status {
        /**
         * 未加载
         */
        EXCLUDE,
        /**
         * 失败
         */
        FAILED,
        /**
         * 成功
         */
        SUCCESS
    }

    @Data
    public static class DeploymentId {
        private String verticleName;
        private String deploymentId;
        private Throwable exception;
        private Enum<DeploymentResult.status> status;

        public DeploymentId(String name, Enum<DeploymentResult.status> status) {
            this.verticleName = name;
            this.status = status;
        }

        @Override
        public String toString() {
            return String.format("%s{name=[%s] %s %s}",
                    this.status.name(),
                    this.verticleName,
                    this.deploymentId == null ? "" : ",deployId=" + this.deploymentId,
                    this.exception == null ? "" : ",exception=" + exception.getMessage());

        }
    }


}
